package com.group.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbBasedataMapper;
import com.group.pojo.Common;
import com.group.pojo.TbBasedata;
import com.group.pojo.TbBasedataExample;
import com.group.pojo.TbRole;
import com.group.pojo.TbBasedataExample.Criteria;
import com.group.service.BasedataService;
import com.group.service.CommonService;
import com.group.util.CommonUtil;

@Service
public class BasedataServiceImpl implements BasedataService{

	@Autowired
	private TbBasedataMapper basedataMapper;
	@Autowired
	private CommonService commonService;

	/**
	 * 查询设置的条件
	 * @param criteria 逆向工程example内部类
	 * @param basedata 条件
	 */
	private void setCondition(Criteria criteria, TbBasedata basedata) {
		// TODO Auto-generated method stub
		String id = basedata.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		Integer level1Id = basedata.getLevel1Id();
		if (level1Id != null) {
			criteria.andLevel1IdEqualTo(level1Id);
		}
		
		String level1Name = basedata.getLevel1Name();
		if (level1Name != null && !"".equals(level1Name)) {
			criteria.andLevel1NameEqualTo(level1Name);
		}
		
		Integer level2Id = basedata.getLevel2Id();
		if (level2Id != null) {
			criteria.andLevel2IdEqualTo(level2Id);
		}
		
		String level2Name = basedata.getLevel2Name();
		if (level2Name != null && !"".equals(level2Name)) {
			criteria.andLevel2NameEqualTo(level2Name);
		}
		
		String level2Value = basedata.getLevel2Value();
		if (level2Value != null && !"".equals(level2Value)) {
			criteria.andLevel2ValueEqualTo(level2Value);
		}
		
		String dataStatus = basedata.getDataStatus();
		if (dataStatus != null && !"".equals(dataStatus)) {
			criteria.andDataStatusEqualTo(dataStatus);
		}
	}
	@Override
	public List<TbBasedata> query(TbBasedata basedata) {
		TbBasedataExample example = new TbBasedataExample();
		Criteria criteria = example.createCriteria();
		
		setCondition(criteria, basedata);
		
		List<TbBasedata> list = basedataMapper.selectByExample(example);
		return list;
	}
	@Override
	public Map<String, String> save(TbBasedata basedata) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		TbBasedataExample example = new TbBasedataExample();
		example.createCriteria().andLevel1NameEqualTo(basedata.getLevel1Name());
		
		List<TbBasedata> selectByExample = basedataMapper.selectByExample(example);
		if (selectByExample.size() == 0) {
			//说明根本没有此大类，而大类id在编程的时候是一定要用的，新建一个大类信息没有任何的意义
			return returnMap;
		}
		TbBasedata lastBasedata = selectByExample.get(0);
		for (TbBasedata tbBasedata : selectByExample) {
			if (tbBasedata.getLevel2Id() > lastBasedata.getLevel2Id()) {
				lastBasedata = tbBasedata;
			}
		}
		//设置level1id、level2id、状态
		basedata.setLevel1Id(lastBasedata.getLevel1Id());
		basedata.setLevel2Id(lastBasedata.getLevel2Id()+1);
		basedata.setDataStatus("1");
		
		//而id和这些小类值不一定要管理，可以断续
		Common common = new Common("tb_basedata", CommonUtil.getCurrentDate("yyyyMMdd"));
		String nextId = commonService.getTbCurrentDateNextId(common);
		basedata.setId(nextId);
		
		//提交到数据库
		int insert = basedataMapper.insert(basedata);
		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}
	@Override
	public Map<String, String> update(TbBasedata basedata) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		//因为小类值可填可不填，在前端无法验证是否已经存在，只有在后台验证
		TbBasedataExample example = new TbBasedataExample();
		Criteria criteria = example.createCriteria();
		
		setCondition(criteria, basedata);
		//小类值还要重新赋值,因为本来有小类值，后台没有了，此时允许更新
		String level2Value = basedata.getLevel2Value();
		if (level2Value == null || "".equals(level2Value)){
			criteria.andLevel2ValueEqualTo("");
		}
		
		int count = basedataMapper.countByExample(example);
		if (count != 0) {
			returnMap.put("msg", "该小类记录已经存在，请重新填写");
			return returnMap;
		}
		//更新合法
		int update = basedataMapper.updateByPrimaryKey(basedata);
		if (update == 1) {
			returnMap.put("success","true");
			return returnMap;
		}
		returnMap.put("msg","保存失败");
		return returnMap;
	}
	@Override
	public Map<String, String> batchDelete(String[] basedataIds) {
		TbBasedata recode = new TbBasedata();
		recode.setDataStatus("0");
		
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		for (String basedataId : basedataIds) {
			recode.setId(basedataId);
			int update = basedataMapper.updateByPrimaryKeySelective(recode);
			if (update != 1) {
				map.put("success", "false");
			}
		}
		return map;
	}
}
